package com.example.service.impl;

import com.example.mapper.DeptMapper;
import com.example.pojo.Dept;
import com.example.pojo.DeptLog;
import com.example.service.DeptLogService;
import com.example.service.DeptService;
import com.example.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private EmpService empService;

    @Autowired
    private DeptLogService deptLogService;
    @Override
    public List<Dept> selectAll() {
        return deptMapper.selectAll() ;
    }

    @Override
    @Transactional(rollbackFor = ParseException.class)

    //在实现类上，提交事务
    public int deleteById(Integer deptid) throws ParseException {

        int i = deptMapper.deleteById(deptid);

       // int a=1/0;
        int i2=empService.delByDeptId(deptid);
        deptLogService.insert(new DeptLog(null,LocalDateTime.now(),i+","+i2));
        //受检时异常，事务不会回滚，所以添加(rollbackFor = ParseException.class)指定异常回滚
     //  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //解析异常是受检异常
     //   Date date = sdf.parse("sduifyt87634hgjsdf");
        return i;

    }

    @Override
    public int addDept(Dept dept) {
        //添加两个对象
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        return deptMapper.addDept(dept);
    }

    @Override
    public Dept selectById(Integer id) {

        return deptMapper.selectById(id);
    }

    @Override
    public int updateDept(Dept dept) {
        return deptMapper.updateDept(dept);
    }

}
